package com.example.seakdreamandroid.bonus;

import static com.example.seakdreamandroid.Constant.BASE_URL;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.seakdreamandroid.Constant;
import com.example.seakdreamandroid.MainActivity;
import com.example.seakdreamandroid.MemberActivity;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.entity.User;
import com.example.seakdreamandroid.lottery.Lottery;
import com.example.seakdreamandroid.sleep.StartSleep;
import com.example.seakdreamandroid.whiteNoise.WhiteNoiseActivity;

import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ExchangeBonusAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private BonusPointsActivity context;
    List<ExchangeBonus> list;
    private User user;
    private FormBody formBody;
    private String userPoints;

    public ExchangeBonusAdapter(BonusPointsActivity context, List<ExchangeBonus> list, User user) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.list = list;
        this.user = user;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ExchangeBonusAdapter.ViewHolder holder = null;
        user = (User) context.getApplication();
        System.out.println(user.toString());
        if (convertView == null) {
            //绑定Item布局
            convertView = layoutInflater.inflate(R.layout.activity_bonus_exchange_item, null, false);
            //自定义内部类，对象holder用来存储文字和图片控件
            holder = new ViewHolder();

            holder.imageView = (ImageView)convertView.findViewById(R.id.exchange_img);
            holder.content = (TextView)convertView.findViewById(R.id.exchange_content);
            holder.bonus = (TextView)convertView.findViewById(R.id.exchange_bonus);


            //将holder放入当前视图中
            convertView.setTag(holder);
        } else {
            //复用holder
            holder = (ExchangeBonusAdapter.ViewHolder) convertView.getTag();
        }
        //取出app对象
        ExchangeBonus commodity=list.get(position);

        Button button = (Button) convertView.findViewById(R.id.exchange_btn);
        holder.content.setText(commodity.getContent());
        holder.bonus.setText(commodity.getBonus()+"积分");
        button.setText("兑换");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str ="?";

                int i = user.getPoints();
                switch (commodity.getBonus()){
                    case 100:
                        user.setVipTime("1");
                        formBody = new FormBody.Builder()
                                .add("userId",user.getUserId())
                                .add("vipTime",user.getVipTime())
                                .build();
                        System.out.println(user.getVipTime());
                        System.out.println(user.getUserId());
                        context.setNewPoints(i-100);
                        postAsync(str,formBody);
                        break;
                    case 150:
                        user.setVipTime("3");
                        formBody = new FormBody.Builder()
                                .add("userId",user.getUserId())
                                .add("vipTime",user.getVipTime())
                                .build();
                        context.setNewPoints(i-150);
                        postAsync(str,formBody);
                        break;
                    case 250:
                        user.setVipTime("7");
                        formBody = new FormBody.Builder()
                                .add("userId",user.getUserId())
                                .add("vipTime",user.getVipTime())
                                .build();
                        context.setNewPoints(i-250);
                        postAsync(str,formBody);
                        break;
                    case 400:
                        user.setVipTime("15");
                        formBody = new FormBody.Builder()
                                .add("userId",user.getUserId())
                                .add("vipTime",user.getVipTime())
                                .build();
                        context.setNewPoints(i-400);
                        postAsync(str,formBody);
                        break;
                    case 60:
                        Intent intent = new Intent();
                        intent.setClass(context, Lottery.class);
                        context.startActivity(intent);
                        break;
                    case 800:
                        context.setNewPoints(i-800);
                }
            }
        });
        return convertView;
    }
    //内部类
    class ViewHolder {
        ImageView imageView;
        TextView content;
        TextView bonus;
        Button button;
    }



    private void postAsync(String s, FormBody body) {
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建请求对象（Request）
        Request request = new Request.Builder()
                //设置
                .url(Constant.BASE_URL+"UserInfo/openVip")
                .post(body)//设置请求方式为post，添加了请求体
                //创建Request对象
                .build();
        //发送请求，获取响应
        //(一个call对象对应一个response)
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("okHttp","onfilure");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //修改用户界面，需要使用Handler或RunOnUiThread
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        context.setTotalPoint();
                        Log.e("okHttp","onSuccess");
                        Toast toast = Toast.makeText(context,"兑换成功",Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                });
            }
        });

    }



}
